package com.situ.blog.dao.impl;

import com.situ.blog.dao.IBlogDao;
import com.situ.blog.pojo.Blog;
import com.situ.blog.pojo.query.BlogQuery;
import com.situ.blog.util.JDBCUtils;
import com.situ.blog.util.JDBCutil;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;

import java.sql.*;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public class BlogDaoImpl implements IBlogDao {
    JdbcTemplate template = new JdbcTemplate(JDBCUtils.getDataSource());
    @Override
    public List<Blog> selectByPage(BlogQuery blogQuery) {
        List<Blog> list = new ArrayList<>();
        List<Object> queryList = new ArrayList<>();

        String sql = "SELECT id,name,image,click_count,content,type_id,status,is_deleted,create_time,update_time FROM blog WHERE 1=1";
        String queryName = blogQuery.getName();
        if (queryName != null && !"".equals(queryName)) {
            sql += " AND name LIKE ?";
            queryList.add("%" + queryName + "%");
        }
        Integer queryTypeId = blogQuery.getTypeId();
        if (queryTypeId != null && !"".equals(queryTypeId)) {
            sql += " AND type_id=?";
            queryList.add(queryTypeId);
        }
        String beginCreateTime = blogQuery.getBeginCreateTime();
        String endCreateTime = blogQuery.getEndCreateTime();
        if (beginCreateTime != null && endCreateTime != null ) {
            sql += " AND create_time BETWEEN ? AND ?";
            queryList.add(beginCreateTime);
            queryList.add(endCreateTime);
        }
        sql+=" LIMIT ?,?";

        int offset = (blogQuery.getPage()-1)*blogQuery.getLimit();
        queryList.add(offset);
        queryList.add(blogQuery.getLimit());

        return template.query(sql,new BeanPropertyRowMapper<>(Blog.class),queryList.toArray());
    }

    @Override
    public int totalCount(BlogQuery blogQuery) {
        List<Object> queryList = new ArrayList<>();
        String queryName = blogQuery.getName();
        String sql = "SELECT count(*) FROM blog WHERE 1=1";
        if (queryName != null && !"".equals(queryName)) {
            sql += " AND name LIKE ?";
            queryList.add("%" + queryName + "%");
        }
        Integer queryTypeId = blogQuery.getTypeId();
        if (queryTypeId != null && !"".equals(queryTypeId)) {
            sql += " AND type_id=?";
            queryList.add(queryTypeId);
        }
        String beginCreateTime = blogQuery.getBeginCreateTime();
        String endCreateTime = blogQuery.getEndCreateTime();
        if (beginCreateTime != null && endCreateTime != null && !"".equals(beginCreateTime)&& !"".equals(endCreateTime)) {
            sql += " AND create_time BETWEEN ? AND ?";
            queryList.add(beginCreateTime);
            queryList.add(endCreateTime);
        }
        return template.queryForObject(sql, Integer.class, queryList.toArray());
    }

    @Override
    public void add(Blog blog) {
        System.out.println("BlogDaoImpl.add");
        System.out.println(blog);

        String sql = "INSERT INTO blog(name,image,click_count,content,type_id,status) VALUES (?,?,?,?,?,?)";
        template.update(sql,blog.getName(),blog.getImage(),blog.getClickCount(),blog.getContent(),blog.getTypeId(),blog.getStatus());


    }
    public int deleteById(int id) {
        System.out.println("删除函数内传过来的id是："+id);

        int count = 0;

            String sql = "DELETE FROM blog WHERE id=?";
            count = template.update(sql,id);

            System.out.println("count:"+count);
            return count;
    }

    @Override
    public Blog selectById(String id) {
        System.out.println("BlogDaoImpl.selectById");
        String sql = "SELECT id,name,image,click_count,content,type_id,status FROM blog WHERE id=?";
        return template.queryForObject(sql, new BeanPropertyRowMapper<>(Blog.class),id);
    }

    @Override
    public void updateStatus(String id,int status) {
        //写sql语句
        String sql = "UPDATE blog SET status=? WHERE id=?";
        template.update(sql,status,id);
    }

    @Override
    public void update(Blog blog) {
        //写sql语句
        String sql = "UPDATE blog SET name=?,type_id=?,image=?,click_count=?,content=? WHERE id=?";
        template.update(sql,blog.getName(),blog.getTypeId(),blog.getImage(),blog.getClickCount(),blog.getContent(),blog.getId());
    }

    @Override
    public List<Blog> selectAll() {
        String sql = "SELECT id,name,image,click_count,type_id,status,is_deleted,create_time FROM blog";
        return template.query(sql, new BeanPropertyRowMapper<>(Blog.class));
    }

    @Override
    public void addExcel(Blog blog) {
        String sql = "INSERT INTO blog (name,image,click_count,type_id,status,create_time) values(?,?,?,?,?,?)";
        template.update(sql,blog.getName(),blog.getImage(),blog.getClickCount(),blog.getTypeId(),blog.getStatus(),blog.getCreateTime());
    }
}
